package com.wtx.chapter05;

/**
 * @description: leetcode 203的虚拟头结点解法
 * @author: wtx
 * @createDate: 2020/5/19
 */
public class Solution2 {

    public ListNode removeElements(ListNode head, int val) {

        //使用虚拟头节点
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;

        ListNode prev = dummyNode;
        while ( prev.next!=null ){

            if (prev.next.val == val)
                prev.next = prev.next.next;
            else
                prev = prev.next;
        }

        //不能直接返回head，因为原来的head可能被删掉了
        return dummyNode.next;
    }
}
